import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    // 商品数组
    goodList: [
      {
        name: "诸葛亮",
        price: 1000,
        num: 1,
        checked: false,
      },
      {
        name: "蔡文姬",
        price: 1500,
        num: 1,
        checked: false,
      },
      {
        name: "妲己",
        price: 2000,
        num: 1,
        checked: false,
      },
      {
        name: "鲁班",
        price: 2200,
        num: 1,
        checked: false,
      },
    ],
  },
  mutations: {
    // 删除方法
    delGoods: (state, index) => state.goodList.splice(index, 1),
    // 添加方法
    addGoods: (state, obj) => state.goodList.push(obj)
  },
  getters: {
    // 统计总价方法
    count: state => state.goodList.reduce((sum, item) => item.checked ? sum + item.price * item.num : sum, 0),

  }
})

export default store